#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "Scenes/Splash/SplashManager.h"
#include "Scenes/Menu/MenuManager.h"
#include "Data/GameData.h"
#include "SimpleAudioEngine.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "IAP/SoomlaIAP.h"
#include "IAP/SoomlaIAPEventHandler.h"
#include "Cocos2dxStore.h"
#endif

USING_NS_CC;

AppDelegate::AppDelegate() {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	handler = new SoomlaIAPEventHandler();
#endif
}

AppDelegate::~AppDelegate()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	soomla::CCStoreEventDispatcher::getInstance()->removeEventHandler(handler);
#endif
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
	//set OpenGL context attributions,now can only set six attributions:
	//red,green,blue,alpha,depth,stencil
	GLContextAttrs glContextAttrs = { 8, 8, 8, 8, 24, 8 };

	GLView::setGLContextAttrs(glContextAttrs);
}

bool AppDelegate::applicationDidFinishLaunching() {
	// initialize director
	auto director = Director::getInstance();
	auto glview = director->getOpenGLView();
	if (!glview) {
		glview = GLViewImpl::create("My Game");
		director->setOpenGLView(glview);	
	}
	// glview->setFrameSize(512, 384);
	glview->setDesignResolutionSize(2048, 1536, ResolutionPolicy::FIXED_HEIGHT);
    //FileUtils::getInstance()->addSearchPath("audio");
	FileUtils::getInstance()->addSearchPath("database");
	FileUtils::getInstance()->addSearchPath("res");
	FileUtils::getInstance()->addSearchPath("src");
    FileUtils::getInstance()->addSearchPath("indicator");
	FileUtils::getInstance()->addSearchPath("src/Background");
	FileUtils::getInstance()->addSearchPath("src/Banner");
	FileUtils::getInstance()->addSearchPath("src/Button");
	FileUtils::getInstance()->addSearchPath("src/Frame");
	FileUtils::getInstance()->addSearchPath("src/Icon");
	FileUtils::getInstance()->addSearchPath("src/Render");
	FileUtils::getInstance()->addSearchPath("src/Fonts");

	FileUtils::getInstance()->addSearchPath("src/Ghost/face/Full");
	FileUtils::getInstance()->addSearchPath("src/Ghost/face/Free");
	FileUtils::getInstance()->addSearchPath("src/Ghost/body");
	FileUtils::getInstance()->addSearchPath("src/Ghost/eye");
	FileUtils::getInstance()->addSearchPath("src/Ghost/eye/lids");
	FileUtils::getInstance()->addSearchPath("src/Ghost/hand");

    CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("bg_music_loop.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("aww.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("button_click.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("pages_swipe.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("settingsbutton_click.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("star.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("yeah.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("add.mp3");
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("minus.mp3");
#pragma region IAP
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	    SoomlaIAP *assets = SoomlaIAP::create();

	    __Dictionary *commonParams = __Dictionary::create();
	    commonParams->setObject(__String::create("ExampleCustomSecret"), "customSecret");
	    soomla::CCServiceManager::getInstance()->setCommonParams(commonParams);

	    __Dictionary *storeParams = __Dictionary::create();
	    storeParams->setObject(__String::create("ExamplePublicKey"), "androidPublicKey");
	    storeParams->setObject(__Bool::create(true), "testPurchases"); //for test

	    soomla::CCStoreService::initShared(assets, storeParams);

	    __Array *currencies =
	                soomla::CCStoreInfo::sharedStoreInfo()->getCurrencies();
	        Ref *currencyObject;
	        CCARRAY_FOREACH(currencies, currencyObject) {
	                soomla::CCVirtualCurrency *vc =
	                        dynamic_cast<soomla::CCVirtualCurrency *>(currencyObject);
	                int balance = soomla::CCStoreInventory::sharedStoreInventory()->
	                        getItemBalance(vc->getItemId()->getCString(), NULL);
	                if (balance < 1000) {
	                    soomla::CCStoreInventory::sharedStoreInventory()->
	                            giveItem(vc->getItemId()->getCString(), 10000 - balance, NULL);
	                }
	            }

	    soomla::CCStoreEventDispatcher::getInstance()->addEventHandler(handler);
		#endif
#pragma endregion

	// turn on display FPS
	director->setDisplayStats(true);

	// set FPS. the default value is 1.0/60 if you don't call this
	director->setAnimationInterval(1.0 / 60);

	// create a scene. it's an autorelease object
	auto scene = SplashManager::createScene();

	// run
	director->runWithScene(scene);

	return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
	Director::getInstance()->stopAnimation();

	// if you use SimpleAudioEngine, it must be pause
	// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
	Director::getInstance()->startAnimation();

	// if you use SimpleAudioEngine, it must resume here
	// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
